-- 这是一个Lua字节码编译器示例，演示如何将Lua源代码编译为字节码文件
-- 示例功能：读取Lua源文件，编译为字节码，并保存为.luac文件

local Globals = require('lua.vm.Globals')
local FileOutputStream = require('lua.io.FileOutputStream')
local DumpState = require('lua.vm.compiler.DumpState')
local GlobalsUnDumper = require('lua.vm.GlobalsUnDumper')
local LuaCompiler = require('lua.vm.compiler.LuaCompiler')
local LuaLoader = require('lua.vm.compiler.LuaLoader')
local CfgState = require('lua.cfg.CfgState')

local workDir = 'E:\\lua Project\\'
local inputPath = workDir .. 'test.lua'
local outputPath = workDir .. 'test.luac'
local globals = Globals:new()
globals:installCompiler(LuaCompiler)
globals:installLoader(LuaLoader)
globals:installUnDumper(GlobalsUnDumper)
local f = globals:loadfile(inputPath)
local p = f:checkClosure().p
local fos = FileOutputStream:new(outputPath)
DumpState:dump(p, fos)
fos:close()
local cfgState = CfgState:new(p.code)
local g = cfgState:analyze()
print(g:toDotString())